import numpy
import cv2
import copy
import random

bg_h, bg_w = 300, 1600
bg = cv2.cvtColor(
    numpy.zeros((bg_h, bg_w),dtype=numpy.uint8),
    cv2.COLOR_GRAY2RGB
)

def PutImageOnBackground(cvrColorIMG, h:int, w:int):
    if (h*1.0)/bg_h > (w*1.0)/bg_w:
        h_dominant = True
    else:
        h_dominant = False
    
    # if h_dominant:
    #     target_h, target_w = int(bg_h/2), int(w*(bg_h/h))
    # else:
    #     target_h, target_w = int(h*(bg_w/w)), int(bg_w/2)
    # modify at 230720
    if h_dominant:
        target_h = int(bg_h/2)
        target_w = int((1.0*w*target_h)/h)
    else:
        target_w = int(bg_w/2)
        target_h = int((1.0*h*target_w)/w)
    
    h_max, w_max = (bg_h-target_h), (bg_w-target_w)
        
    resized0 = cv2.resize(
        cvrColorIMG, 
        dsize=(target_w, target_h), 
        # dsize 形参数组宽度在前，高度在后，
        # https://blog.csdn.net/qq_42079689/article/details/102537600
        interpolation=cv2.INTER_LINEAR
    )

    delta_h, delta_w = int(random.random()*h_max), int(random.random()*w_max)
    resized1 = copy.deepcopy(bg)
    #嵌入图片，resized1是原图，resized0是烟雾图片，中括号内为嵌入的坐标
    resized1[delta_h:target_h+delta_h, delta_w:target_w+delta_w] = resized0
    
    # cv2.imwrite('temp1.jpg', resized1)
    # input()
        
    return resized1, (delta_h, delta_w), (target_h, target_w)

